/*
 * Copyright (C) 2009 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */

package com.kindac.together.widget;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.CharArrayBuffer;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

import com.kindac.together.R;
import com.kindac.together.common.Kin;
import com.kindac.together.db.KinDB;
import com.kindac.together.uitl.Utils;

/**
 * @author Kindac
 * @time 2011-6-21 上午01:27:00
 */
public class LolActivity extends ListActivity {

    /** meun */
    private static final int ADD_ID = Menu.FIRST;

    private final class ContactListItemAdapter extends ResourceCursorAdapter {

        public ContactListItemAdapter(Context context, int layout, Cursor c) {
            super(context, layout, c);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            final ContactListItemCache cache = (ContactListItemCache) view.getTag();
            // Set the name
            cursor.copyStringToBuffer(1, cache.postBuffer);
            int size = cache.postBuffer.sizeCopied;
            cache.post.setText(cache.postBuffer.data, 0, size);

            cursor.copyStringToBuffer(2, cache.timeBuffer);
            size = cache.timeBuffer.sizeCopied;
            cache.time.setText(cache.timeBuffer.data, 0, size);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = super.newView(context, cursor, parent);
            ContactListItemCache cache = new ContactListItemCache();
            cache.post = (TextView) view.findViewById(R.id.t_post);
            cache.time = (TextView) view.findViewById(R.id.t_time);
            view.setTag(cache);
            return view;
        }
    }

    final static class ContactListItemCache {

        public TextView        post;
        public TextView        time;
        public CharArrayBuffer postBuffer = new CharArrayBuffer(128);
        public CharArrayBuffer timeBuffer = new CharArrayBuffer(128);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        KinDB db = KinDB.getInstance(LolActivity.this);
        Cursor c = db.getPost(LolActivity.this);
        startManagingCursor(c);
        ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.list, c);
        setListAdapter(adapter);

        Utils.MessageBox(LolActivity.this, getResources().getString(R.string.text_success));

    }

    // @Override
    public void onAttachedToWindow() {
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }
    /**
     * 系统级菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean b = super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, ADD_ID, Menu.NONE, R.string.add_post).setIcon(android.R.drawable.ic_menu_edit);
        return b;
    }

    /**
     * 系统级菜单事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case ADD_ID:
                startActivity(new Intent(LolActivity.this, LolPostActivity.class).setAction(Kin.ADD));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Cursor c = (Cursor) l.getAdapter().getItem(position);

        Intent i = new Intent(LolActivity.this, LolPostActivity.class);
        i.setAction(Kin.MOD);
        i.putExtra("id", c.getInt(0));
        i.putExtra("post", c.getString(1));
        startActivity(i);
    }

}
